package com.chuanye.jiguangimdemo.activitys;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.chuanye.jiguangimdemo.R;
import com.chuanye.jiguangimdemo.database.FriendRecommendEntry;
import com.chuanye.jiguangimdemo.database.UserEntry;
import com.chuanye.jiguangimdemo.entity.FriendInvitation;
import com.chuanye.jiguangimdemo.model.InfoModel;

import cn.jpush.im.android.api.ContactManager;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.api.BasicCallback;

/**
 * 验证消息发送页面
 */
public class VerificationActivity extends BaseActivity implements View.OnClickListener {
    private static final String TAG = "VerificationActivity";
    private Context mContext;

    private ImageView verification_back_iv;
    private TextView verification_send_tv;
    private EditText verification_content_et;

    private UserInfo mMyInfo;
    private String mTargetAppKey;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verification);
        mContext = this;

        initView();

    }

    @SuppressLint("WrongConstant")
    private void initView() {
        //返回
        verification_back_iv = findViewById(R.id.verification_back_iv);
        verification_back_iv.setOnClickListener(this);
        //发送
        verification_send_tv = findViewById(R.id.verification_send_tv);
        verification_send_tv.setOnClickListener(this);
        //输入框
        verification_content_et = findViewById(R.id.verification_content_et);

        mMyInfo = JMessageClient.getMyInfo();
        mTargetAppKey = mMyInfo.getAppKey();

        String name;
        //群组详细信息点击非好友头像,跳转到此添加界面
        if (getIntent().getFlags() == 1) {
            name = getIntent().getStringExtra("detail_add_friend_my_nickname");
            if (TextUtils.isEmpty(name)) {
                verification_content_et.setText("我是");
            } else {
                verification_content_et.setText("我是" + name);
            }
            //搜索用户发送添加申请
        } else {
            name = mMyInfo.getNickname();
            if (TextUtils.isEmpty(name)) {
                verification_content_et.setText("我是");
            } else {
                verification_content_et.setText("我是" + name);
            }
        }




    }

    @SuppressLint("WrongConstant")
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.verification_back_iv://返回
                finish();
                break;
            case R.id.verification_send_tv://发送
                final String userName;
                String displayName;
                String targetAvatar;
                Long targetUid;
                if (getIntent().getFlags() == 1) {
                    //添加好友申请时对方信息
                    userName = getIntent().getStringExtra("detail_add_friend");
                    displayName = getIntent().getStringExtra("detail_add_nick_name");
                    targetAvatar = getIntent().getStringExtra("detail_add_avatar_path");
                    targetUid = getIntent().getLongExtra("detail_add_uid", 0);
                    if (TextUtils.isEmpty(displayName)) {
                        displayName = userName;
                    }
                    //搜索方式添加好友
                } else {
                    targetAvatar = InfoModel.getInstance().getAvatarPath();
                    displayName = InfoModel.getInstance().getNickName();
                    targetUid = InfoModel.getInstance().getUid();
                    if (TextUtils.isEmpty(displayName)) {
                        displayName = InfoModel.getInstance().getUserName();
                    }
                    userName = InfoModel.getInstance().getUserName();
                }

                final String reason = verification_content_et.getText().toString();
                final String finalTargetAvatar = targetAvatar;
                final String finalDisplayName = displayName;
                final Long finalUid = targetUid;
                ContactManager.sendInvitationRequest(userName, "", reason, new BasicCallback() {
                    @Override
                    public void gotResult(int responseCode, String responseMessage) {
                        Log.i(TAG,"发送添加好友：返回码：" + responseCode + " ,返回内容:" + responseMessage);
                        if (responseCode == 871317){
                            Toast.makeText(mContext,"不能添加自己为好友",Toast.LENGTH_SHORT).show();
                        }else if (responseCode == 0){

                            UserEntry userEntry = UserEntry.getUser(mMyInfo.getUserName(), mMyInfo.getAppKey());
                            FriendRecommendEntry entry = FriendRecommendEntry.getEntry(userEntry, userName, mTargetAppKey);
                            if (null == entry) {
                                entry = new FriendRecommendEntry(finalUid, userName, "", finalDisplayName, mTargetAppKey,
                                        finalTargetAvatar, finalDisplayName, reason, FriendInvitation.INVITING.getValue(), userEntry, 100);
                            } else {
                                entry.state = FriendInvitation.INVITING.getValue();
                                entry.reason = reason;
                            }
                            entry.save();

                            Toast.makeText(mContext,"发送成功",Toast.LENGTH_SHORT).show();
                            finish();
                        }else{
                            Toast.makeText(mContext,"申请失败",Toast.LENGTH_SHORT).show();
                            finish();
                        }

                    }
                });

                break;
        }
    }
}
